package bankowosc.polaczenie;

import bankowosc.dane.Klient;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import bankowosc.dane.Umowa;

public class UmowaBD {

	public ArrayList<Umowa> pobierzUmowy(polacz p, int klient) {
		ArrayList<Umowa> umowy = new ArrayList<>();
		Statement stmt = p.getStmt();
		String komenda = "select * from umowa where id_klienta="+klient;
		try {
			ResultSet rs = stmt.executeQuery(komenda);
			while(rs.next()) {
				umowy.add(new Umowa(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5)));
			}
			rs.close();
			return umowy;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
        public Klient pobierzWspolwlasciciela(polacz p, String rachunek) {
		ArrayList<Umowa> umowy = new ArrayList<>();
		Statement stmt = p.getStmt();
                Integer id = null;
                Klient k = new Klient();
		String komenda = "select id_klienta from umowa where nr_rachunku="+rachunek+"and rodzaj_klienta='Wspolwlasciciel'";
		try {
			ResultSet rs = stmt.executeQuery(komenda);
			while(rs.next()) {
				id=rs.getInt(1);
                        };
			rs.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
                String komenda2 ="select * from klient where id_klienta="+id;
                try {
			ResultSet rs = stmt.executeQuery(komenda2);
			while(rs.next()) {
				k=new Klient(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),
						rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12),
						rs.getString(13),rs.getString(14),rs.getString(15),rs.getString(16),rs.getString(17),rs.getString(18),
						rs.getString(19),rs.getInt(20),rs.getInt(21),rs.getString(22));
				 };
			rs.close();
			return k;
		} catch (SQLException e) {
			e.printStackTrace();
		}
                
                
		return null;
	}
        public Klient pobierzWlasciciela(polacz p, String rachunek) {
		ArrayList<Umowa> umowy = new ArrayList<>();
		Statement stmt = p.getStmt();
                Integer id = null;
                Klient k = new Klient();
		String komenda = "select id_klienta from umowa where nr_rachunku="+rachunek+"and rodzaj_klienta='Wlasciciel'";
		try {
			ResultSet rs = stmt.executeQuery(komenda);
			while(rs.next()) {
				id=rs.getInt(1);
                        };
			rs.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
                String komenda2 ="select * from klient where id_klienta="+id;
                try {
			ResultSet rs = stmt.executeQuery(komenda2);
			while(rs.next()) {
				k=new Klient(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),
						rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12),
						rs.getString(13),rs.getString(14),rs.getString(15),rs.getString(16),rs.getString(17),rs.getString(18),
						rs.getString(19),rs.getInt(20),rs.getInt(21),rs.getString(22));
				 };
			rs.close();
			return k;
		} catch (SQLException e) {
			e.printStackTrace();
		}
                
                
		return null;
	}

}